package com.ad.pages;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.tapestry5.annotations.Id;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.RequestGlobals;
import org.apache.tapestry5.util.TextStreamResponse;

import com.ad.base.AbstractPage;
import com.ad.hibernate.TipType;
import com.ad.services.IHttpService;
import com.ad.services.ITipManagerService;
import com.ad.services.IUserManagerService;
import com.ad.system.Common;

public class AjaxServer extends AbstractPage {

	private static Logger log = Logger.getLogger(AjaxServer.class);

	@Inject
	private RequestGlobals request;

	@Inject
	@Id("httpService")
	private IHttpService httpService;

	@Inject
	@Id("userService")
	private IUserManagerService newUserService;

	@Inject
	@Id("tipService")
	private ITipManagerService tipManagerService;

	void beginRender() {

	}

	protected Object onActivate() {
		String type = request.getHTTPServletRequest().getParameter("type");
		// String page = request.getParameter("page");
		if ("weather".equals(type)) {
			return weather();
		} else if ("search".equals(type)) {
			return search();
		} else if ("weather2".equals(type)) {
			return weather2();
		} else if ("validate".equals(type)) {
			return validate();
		} else if ("clear".equals(type)) {
			return clear();
		} else if ("publishSelect".equals(type)) {
			return divType();
		}

		return new TextStreamResponse("text/plain", String.format("%s",
				"未知的操作类型！"));
	}

	private Object weather() {
		String url = request.getHTTPServletRequest().getParameter("url");
		String city = request.getHTTPServletRequest().getParameter("city");

		String weather = "";
		log.info("Getting city(" + city + ")'s weather information from url: "
				+ url);
		weather = httpService.getWeather(url);

		if (Common.isEmpty(weather)) {
			city = "";
		}
		return new TextStreamResponse("text/plain", String.format("%s,%s",
				weather, city));
	}

	private Object weather2() {
		String city = request.getHTTPServletRequest().getParameter("city");
		// log.info("weather,rawCityInfo：" + city);
		try {
			city = Common.chineseDecode(city);
		} catch (IOException e) {
			log.info(e.getMessage());
			e.printStackTrace();
		}

		// log.info("after decode：" + city);
		String weather = httpService.getWeather(
				request.getHTTPServletRequest(), city);
		return new TextStreamResponse("text/plain", String
				.format("%s", weather));
	}

	private Object search() {
		String partNumber = request.getHTTPServletRequest().getParameter("k");
		return new TextStreamResponse("text/plain", String.format("%s",
				partNumber));
	}

	// 以下都是小弟的测试
	private Object validate() {
		String userName = request.getHTTPServletRequest().getParameter(
				"userName");
		if (this.newUserService.Check(userName)) {
			String msm = "1";
			return new TextStreamResponse("text/plain", msm);
		}
		String msm = "0";
		return new TextStreamResponse("text/plain", msm);
	}

	private Object clear() {
		String type = request.getHTTPServletRequest().getParameter("type");

		return null;
	}

	private Object divType() {
		String type = request.getHTTPServletRequest().getParameter("t");
		List<TipType> subTypes = tipManagerService.getChildren(type);
		StringBuffer subName = new StringBuffer();
		StringBuffer subId = new StringBuffer();
		for (int i = 0; i < subTypes.size(); i++) {
			subName.append(subTypes.get(i).getCategoriesName()).append(",");
			subId.append(subTypes.get(i).getId()).append(",");
		}
		return new TextStreamResponse("text/plain", String.format("%s@%s",
				subName, subId));
	}
}